package com.mukefile.action;

import com.mukefile.exception.MsgException;
import com.mukefile.pojo.Log;
import com.mukefile.pojo.MLog;
import com.mukefile.service.LogService;
import com.mukefile.utils.PageInfo;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @Author: kenewstar
 * @Description:
 * @Date:Created in 2020/9/11
 */
@Controller("logAction")
@Scope("prototype")
public class LogAction extends ActionSupport implements ModelDriven<Log> {

    private final Log log = new Log();
    @Override
    public Log getModel() {
        return this.log;
    }
    //获取Request对象
    private Map<String,Object> request = (Map<String,Object>) ActionContext.getContext().get("request");
    //获取session
    private Map<String,Object> session = ActionContext.getContext().getSession();

    @Autowired
    private LogService logService;
    private static final Integer PAGE_SIZE = 5;
    /**
     * 获取页码
     */
    private Integer pageNum;

    public Integer getPageNum() {
        return pageNum;
    }

    public void setPageNum(Integer pageNum) {
        this.pageNum = pageNum;
    }

    /**
     * 默认查询用户前五条日志
     * 显示用户日志(最新的5条，不分管理员、用户)
     */
    public String showUserLog(){
        // 清除条件查询中的三个条件session
        session.remove("username");
        session.remove("startTime");
        session.remove("endTime");
        // 用户标识0
        session.put("log",0);
        // 无查询关键字
        // 查询最新的5条日志
        int pageNum = 0;
        PageInfo<Log> pageInfo = this.logService.findLogLimit(pageNum, PAGE_SIZE);
        // 放入request域
        request.put("pageInfo",pageInfo);
        // 将最大页放入session
        session.put("pageMax",pageInfo.getTotalPages());
        // 跳转页面
        return "showUserLog";
    }

    /**
     * 分页查询
     * @return 返回分页信息
     */
    public String showPageInfoLog(){
        // 获取条件查询信息
        String username = (String) session.get("username");
        Date startTime = (Date) session.get("startTime");
        Date endTime = (Date) session.get("endTime");
        Integer log = (Integer) session.get("log");
        // 获取最大页信息
        Integer pageMax = (Integer) session.get("pageMax");
        //Integer.
        if (this.pageNum<0){
            this.pageNum = 0;
        }else if(this.pageNum>=pageMax){
            this.pageNum = pageMax-1;
        }
        if (log==0){
            PageInfo<Log>  pageInfo = this.logService.findLogByCondition(username, startTime, endTime, this.pageNum, PAGE_SIZE);
            request.put("pageInfo",pageInfo);
        }else {
            PageInfo<MLog> pageInfo = this.logService.findMLogByCondition(username, startTime, endTime, this.pageNum, PAGE_SIZE);
            request.put("pageInfo",pageInfo);
        }

        return "showUserLog";
    }

    private String username;
    private Date startTime;
    private Date endTime;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public Date getStartTime() {
        return startTime;
    }

    public void setStartTime(Date startTime) {
        this.startTime = startTime;
    }

    public Date getEndTime() {
        return endTime;
    }

    public void setEndTime(Date endTime) {
        this.endTime = endTime;
    }

    /**
     * 根据条件查询日志信息
     * @return 返回到日志显示页面
     */
    public String showLogInfoByCondition(){
        // 将条件查询日志的三个条件放入session
        session.put("username",username);
        session.put("startTime",startTime);
        session.put("endTime",endTime);
        Integer log = (Integer) session.get("log");

        if (Objects.nonNull(startTime)&&Objects.nonNull(endTime)&&startTime.after(endTime)){
            throw new RuntimeException("您的结束时间应该在开始时间之前！");
        }
        // 如果pageNum为空，则默认等于第一页
        if (Objects.isNull(pageNum)){
            pageNum = 0;
        }
        if (log == 0) {
            PageInfo<Log> pageInfo = logService.findLogByCondition(username, startTime, endTime, pageNum, PAGE_SIZE);
            request.put("pageInfo",pageInfo);
            session.put("pageMax",pageInfo.getTotalPages());
            System.out.println("000000");
        }else {
            PageInfo<MLog> pageInfo = logService.findMLogByCondition(username, startTime, endTime, pageNum, PAGE_SIZE);
            request.put("pageInfo",pageInfo);
            session.put("pageMax",pageInfo.getTotalPages());
            System.out.println("1111111111");
        }

        return "showUserLog";
    }

    /**
     * *************************************
     * 管理员日志查询操作
     *
     * *****************************************
     */
    /**
     * 显示管理员的日志记录
     * @return 跳转页面
     */
    public String showManagerLog(){
        // 清除条件查询中的三个条件session
        session.remove("username");
        session.remove("startTime");
        session.remove("endTime");
        // 管理员标识1
        session.put("log",1);
        PageInfo<MLog> pageInfo = this.logService.findMLogByCondition(this.username, this.startTime, this.endTime, 0, PAGE_SIZE);
        // 放入request域
        request.put("pageInfo",pageInfo);
        // 将最大页放入session
        session.put("pageMax",pageInfo.getTotalPages());

        return "showUserLog";
    }

}
